package com.amethyst.swagger3.demo.controller;

import com.amethyst.swagger3.demo.common.ResponseResult;
import com.amethyst.swagger3.demo.common.exception.BizException;
import com.amethyst.swagger3.demo.controller.vo.UserVO;
import com.amethyst.swagger3.demo.enums.CodeEnum;
import com.amethyst.swagger3.demo.model.AuthorUser;
import com.amethyst.swagger3.demo.model.User;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import java.util.UUID;

/**
 * @author Amethyst
 * @date 2021/10/11
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/users")
@Slf4j
public class UserController {
    static List<User> users = new ArrayList<>();
    static Long maxLimit = 10L;

    static {
        for (long i = 0; i < maxLimit; i++) {
            User user = new User()
                    .setId(i)
                    .setUsername(UUID.randomUUID().toString())
                    .setAddress("address-" + i)
                    .setPhone("1234567890" + i);
            users.add(user);
        }
    }

    @ApiOperation("获取用户详情")
    @GetMapping("/{id}")
    public ResponseResult<UserVO> getUserDetail(@PathVariable @ApiParam(value = "用户id", readOnly = true) Long id, @RequestParam String token) {
        User user = users.stream().filter(u -> u.getId().equals(id)).findFirst().orElseThrow(() -> new BizException(CodeEnum.DATA_NOT_FOUND));
        UserVO vo = new UserVO();
        BeanUtils.copyProperties(user, vo);
        return ResponseResult.ok(vo);
    }

    @ApiOperation("获取用户列表")
    @GetMapping
    public List<UserVO> getUsers(@RequestParam String token, @ApiIgnore AuthorUser authorUser) {
        log.info("aop返回的用户信息：" + authorUser.toString());
        List<UserVO> vos = new ArrayList<>();
        users.forEach(u -> {
            UserVO vo = new UserVO();
            BeanUtils.copyProperties(u, vo);
            vos.add(vo);
        });
        return vos;
    }

    @ApiOperation("新增用户")
    @PostMapping
    public ResponseResult<Void> addUser(@RequestBody UserVO vo, @RequestParam String token) {
        return ResponseResult.ok();
    }

    @ApiOperation("获取用户集合")
    @ApiImplicitParam(name = "ids", value = "用户ids", allowMultiple = true, dataType = "Long", dataTypeClass = Long.class)
    @GetMapping("/ids")
    public ResponseResult<List<UserVO>> getUsersByIds(@RequestParam Long[] ids, @RequestParam String token, @ApiIgnore AuthorUser authorUser) {
        List<UserVO> vos = new ArrayList<>();
        for (Long id : ids) {
            User user = users.stream().filter(u -> u.getId().equals(id)).findFirst().orElse(null);
            if (user != null) {
                UserVO vo = new UserVO();
                BeanUtils.copyProperties(user, vo);
                vos.add(vo);
            }
        }
        return ResponseResult.ok(vos);
    }

    @ApiOperation("单文件上传")
    @PostMapping("/uploadFile")
    public ResponseResult<String> uploadFile(@RequestParam String name, @RequestPart MultipartFile file, @RequestParam String token) {
        StringJoiner joiner = new StringJoiner(":", "[", "]");
        joiner.add(name);
        joiner.add(file.getOriginalFilename());
        return ResponseResult.ok(joiner.toString());
    }

    @ApiOperation("多文件上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "上传人", dataTypeClass = String.class, paramType = "form"),
            @ApiImplicitParam(name = "files", value = "文件", dataTypeClass = MultipartFile.class, dataType = "__file", allowMultiple = true, paramType = "form")
    })
    @PostMapping(value = "/uploadFiles", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseResult<List<String>> uploadFiles(@RequestParam String name, @RequestPart MultipartFile[] files, @RequestParam String token) {
        List<String> results = new ArrayList<>();
        StringJoiner joiner;
        for (MultipartFile file : files) {
            joiner = new StringJoiner(":", "[", "]");
            joiner.add(name);
            joiner.add(file.getOriginalFilename());
            results.add(joiner.toString());
        }
        return ResponseResult.ok(results);
    }
}
